#include "FightSucceedScene.h"
#include "../../../model/proxy/FightProxy.h"
#include "../EquipIcon.h"
#include "../../../net/NetController.h"

FightSucceedScene::FightSucceedScene()
	:mSellBtn(NULL),mMeltBtn(NULL),mEquipDetail(NULL),mGold(NULL),mExp(NULL)
{
    CCLog("FightSucceedScene::FightSucceedScene");
}

FightSucceedScene::~FightSucceedScene()
{
    CCLog("FightSucceedScene::~FightSucceedScene");
	CC_SAFE_RELEASE(mSellBtn);
	CC_SAFE_RELEASE(mMeltBtn);
	CC_SAFE_RELEASE(mGold);
	CC_SAFE_RELEASE(mExp);
	//CC_SAFE_RELEASE(mItemIcon);
}

cocos2d::extension::SEL_CCControlHandler FightSucceedScene::onResolveCCBCCControlSelector( CCObject * pTarget, CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onReturnBtnClick", FightSucceedScene::onReturnBtnClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onSellBtnClick", FightSucceedScene::onSellBtnClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onMeltBtnClick", FightSucceedScene::onMeltBtnClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onClearBtnClick", FightSucceedScene::onClearBtnClick);
	return NULL;
}

bool FightSucceedScene::onAssignCCBMemberVariable( CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode )
{
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mSellBtn", CCControlButton *, this->mSellBtn);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mMeltBtn", CCControlButton *, this->mMeltBtn);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mGold", CCLabelTTF *, this->mGold);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mExp", CCLabelTTF *, this->mExp);
	return NULL;
}

void FightSucceedScene::onNodeLoaded( CCNode * pNode, CCNodeLoader * pNodeLoader )
{

}

bool FightSucceedScene::init()
{
	return true;
}

void FightSucceedScene::onEnter()
{
	State::onEnter();
	refresh();
}

void FightSucceedScene::onExit()
{
	State::onExit();
}

void FightSucceedScene::refresh()
{
	Award& awardInfo = FightProxy::getSingletonPtr()->awardInfo;
	mGold->setString(fcs("+%d", awardInfo.coin));
	mExp->setString(fcs("+%d", awardInfo.exp));

	EquipInfo* equipInfo = FightProxy::getSingletonPtr()->getAwardEquip();
	if (equipInfo != NULL)
	{
		if (mEquipDetail == NULL)
		{
			mEquipDetail = EquipDetail::create(this);
			mEquipDetail->setPosition(42, 162);
			mEquipDetail->mEquipIcon->setPosition(ccp(3, 40));
			addChild(mEquipDetail);

		}
		mEquipDetail->setData(equipInfo);
		mEquipDetail->refresh();
		mSellBtn->setVisible(true);
		mMeltBtn->setVisible(true);
	}
	else
	{
		mSellBtn->setVisible(false);
		mMeltBtn->setVisible(false);
	}
}

void FightSucceedScene::onReturnBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
	Framework::getSingletonPtr()->changeState("DungeonsScene");
}

void FightSucceedScene::onSellBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
	EquipInfo* equipInfo = FightProxy::getSingletonPtr()->getAwardEquip();
	vector<int>& sellList = ItemProxy::getSingletonPtr()->sellList;
	sellList.clear();
	sellList.push_back(equipInfo->index);
	NetController::getSingletonPtr()->sellEquipage(sellList);
}

void FightSucceedScene::onMeltBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{
	EquipInfo* equipInfo = FightProxy::getSingletonPtr()->getAwardEquip();
	ItemProxy::getSingletonPtr()->mSelectList.clear();
	ItemProxy::getSingletonPtr()->mSelectList.push_back(equipInfo->index);
	Framework::getSingletonPtr()->popup("MeltOkDialog", equipInfo);
}

void FightSucceedScene::onClearBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{

}
